package com.ruoyi.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.admin.domain.entity.SupplierModel;
import com.ruoyi.admin.domain.vo.response.supplier.SupplierPageResponse;
import com.ruoyi.admin.domain.vo.response.supplier.SupplierResponse;
import com.ruoyi.admin.mapper.SupplierMapper;
import com.ruoyi.admin.service.SupplierService;
import com.ruoyi.common.api.Result;
import com.ruoyi.common.api.ResultCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 供应商客户表(Supplier)Service实现类
 *
 * @author jianhan
 * @date 2023-01-31 09:53:26
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, SupplierModel> implements SupplierService {

    private static final Logger logger = LoggerFactory.getLogger(SupplierServiceImpl.class);

    @Override
    public Result<SupplierResponse> getId(Long supplierId) {
        SupplierModel supplierModel = this.getById(supplierId);
        if (supplierModel != null) {
            SupplierResponse supplierResponse = new SupplierResponse(supplierModel);
            return Result.success(supplierResponse);
        }
        return Result.failed(ResultCode.DATA_NOT_EXIST);
    }

    @Override
    public Result<SupplierPageResponse> page(Page<SupplierModel> page, SupplierModel searchModel) {
        QueryWrapper<SupplierModel> queryWrapper = queryWrapper(searchModel);

        Page<SupplierModel> pages = this.page(page, queryWrapper);
        SupplierPageResponse supplierPageResponse = new SupplierPageResponse(pages);
        page.getRecords().forEach((supplierModel) -> supplierPageResponse.getRecords().add(new SupplierResponse(supplierModel)));
        return Result.success(supplierPageResponse);

    }

    @Override
    public Result add(SupplierModel supplierModel) {
        Boolean result = this.save(supplierModel);
        if (result) {
            return Result.success();
        }
        return Result.failed("添加失败");
    }

    @Override
    public Result update(SupplierModel supplierModel) {
        Boolean result = this.updateById(supplierModel);
        if (result) {
            return Result.success();
        }
        return Result.failed("更新失败");
    }

    @Override
    public Result delete(Long supplierId) {
        Boolean result = this.removeById(supplierId);
        return Result.judge(result);

    }

    private QueryWrapper<SupplierModel> queryWrapper(SupplierModel supplierModel) {
        QueryWrapper<SupplierModel> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(supplierModel.getName())) {
            queryWrapper.like("name", supplierModel.getName());
        }
        if (StringUtils.isNotEmpty(supplierModel.getContactName())) {
            queryWrapper.like("contact_name", supplierModel.getContactName());
        }
        if (StringUtils.isNotEmpty(supplierModel.getAddress())) {
            queryWrapper.like("address", supplierModel.getAddress());
        }
        if (supplierModel.getType() != null) {
            queryWrapper.eq("type", supplierModel.getType());
        }
        
        /*
           写搜索条件
        */
        queryWrapper.orderByDesc("id");
        return queryWrapper;
    }

}
